<?php /* Template Name: Teste */ ?>

<?php 
include_once ABSPATH.'/av-define.php';
include_once PDO.'/NoticiasPDO.class.php';

$us = new NoticiasPDO();
$linha = $us->busca_noticias();
$aux = 0;

foreach ($linha as $key) {
	echo "Titulo: \t" . utf8_encode($key['titulo']) . "<br>\n";
	echo "Descrição: \t".utf8_encode($key['descricao']) . "<br>\n";
	echo "URL: \t ".utf8_encode($key['url']) . "<br>\n";
	echo "Fonte: \t ".utf8_encode($key['fonte']) . "<br>\n";
	echo "Data: \t ".utf8_encode($key['data']) . "<br><br>\n";
	
	
	$noticia_title = utf8_encode($key['titulo']);
	$noticia_descricao = utf8_encode($key['descricao']);
	$noticia_url = utf8_encode($key['url']);
	$noticia_fonte = utf8_encode($key['fonte']);
	$noticia_data = utf8_encode($key['data']);
	
	$category_ID = get_category_id('Notícias');
	$user = get_user_by( 'email', 'conteudo.avespt.com@gmail.com' );
	
	
	// Create post object
	$new_post = array(
			'post_title'   => $noticia_title,
			'post_excerpt' => $noticia_descricao,
			'post_date'    => $noticia_data,
			'post_category' => array( $category_ID ),
			'post_author'   => $user->ID,
			'post_status'  => 'publish' 
	);
	
	$new_post_id = wp_insert_post( $new_post );
	
	
	add_post_meta($new_post_id, 'fonte', $noticia_fonte);
	add_post_meta($new_post_id, 'url', $noticia_url);
	
	$aux = $aux + 1;
	if($aux == 1) {
		echo 'Inseridos: '.$aux.' posts';
		break;
	}
	
}